package cn.edu.usst.cs.goldfish.booking.meetingroom.mybatis;

import cn.edu.usst.cs.goldfish.booking.meetingroom.model.Meeting;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author Jerry
 * @since 2025-06-28
 */


@Mapper
public interface MeetingMapper extends BaseMapper<Meeting> {

    @Select("SELECT * FROM meeting WHERE organizer_id = #{organizerId}")
    List<Meeting> selectByOrganizerId(Long organizerId);

    @Select("SELECT * FROM meeting WHERE status != 'completed' AND id IN (SELECT meeting_id FROM meeting_slot GROUP BY meeting_id HAVING MAX(date + (hour || ':' || ((part-1)*10))::interval) < now())")
    List<Meeting> selectExpiredUncompletedMeetings();

    @Select("SELECT * FROM meeting WHERE status = 'scheduled' AND id IN (SELECT meeting_id FROM meeting_slot GROUP BY meeting_id HAVING MIN(date + (hour || ':' || ((part-1)*10))::interval) BETWEEN now() AND now() + interval '24 hours')")
    List<Meeting> selectMeetingsWithin24Hours();

    @Update("UPDATE meeting SET status = CAST(#{status} AS meeting_status), room_id = #{roomId}, organizer_id = #{organizerId}, topic = #{topic}, price = #{price} WHERE id = #{id}")
    int updateMeeting(@Param("status") cn.edu.usst.cs.goldfish.booking.meetingroom.model.enums.MeetingStatus status,
                     @Param("roomId") Long roomId,
                     @Param("organizerId") Long organizerId,
                     @Param("topic") String topic,
                     @Param("price") java.math.BigDecimal price,
                     @Param("id") Long id);

}
